package com.example.demo.service.impl;

import com.example.demo.entity.basicdata;
import com.example.demo.entity.faultmanage;
import com.example.demo.entity.querymanage;
import com.example.demo.mapper.basicdataMapper;
import com.example.demo.mapper.faultmanageMapper;
import com.example.demo.mapper.querymanageMapper;
import com.example.demo.service.FaultmanageService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class FaultmanageServiceImpl implements FaultmanageService {
    @Resource
    private faultmanageMapper faultmanageMapper;
    @Resource
    private basicdataMapper basicdataMapper;
    @Resource
    private querymanageMapper querymanageMapper;

    @Override
    public String insert(@RequestBody faultmanage faultmanage)//插入故障
    {
        //查询设备是否存在
        basicdata basicdata1=basicdataMapper.SelectWfkeyUkey(faultmanage.getWfkey(),faultmanage.getUkey());
        if (basicdata1!=null)
        {
            if (faultmanageMapper.insert(faultmanage)==1)//故障插入成功
            {
                faultmanageMapper.estatused(faultmanage.getWfkey(),faultmanage.getUkey());//将设备运行状态设为停机
                //插入查询表
                querymanage querymanage1=new querymanage();
                querymanage1.setFkey(faultmanage.getFkey());
                querymanage1.setFname(faultmanage.getFname());
                querymanage1.setFtime(faultmanage.getFmaintaindate());
                querymanage1.setUpower(basicdata1.getUpower());
                querymanage1.setTpower(basicdata1.getTpower());
                if(querymanageMapper.insert(querymanage1)==1)
                {
                    return "插入成功";
                }
                else return "查询管理插入失败";
            }
            else return "插入失败";
        }
        else return "机组不存在";
    }

    @Override
    public String reset(@RequestBody int fkey) throws ParseException {//复位运行
        if (faultmanageMapper.selectResetdate(fkey)==null)//查询故障是否为空
        {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//获取当前时间
            Date date = new Date();
            if (faultmanageMapper.reset(fkey,dateFormat.format(date))==1)//设定复位运行时间
            {
                querymanage querymanage1=querymanageMapper.selectFtime(fkey);//查询查询表中信息
                Date date1=dateFormat.parse(querymanage1.getFtime());//获取设备停机时间
                double time=(((date.getTime() - date1.getTime())/(60.0*60.0*1000.0)));//计算时间差
                System.out.println(date.getTime() - date1.getTime());
                System.out.println(time);
                double ploss=querymanage1.getUpower()*time;//计算发电量损失
                System.out.println(ploss);
                if(querymanageMapper.updatePloss(fkey,ploss)==1)//更新发电量损失
                {
                    faultmanageMapper.estatusing(faultmanageMapper.selectwfu(fkey));//将设备运行状态设为运行
                    return "复位运行成功";
                }
                else return "发电量损失插入失败";
            }
            else return "复位时间插入失败";
        }
        else
            return "设备已运行";
    }

    @Override
    public List query(){return faultmanageMapper.query();}//查询故障信息
    @Override
    public List querysuggestions(int fcode)
    {
        return faultmanageMapper.querysuggestions(fcode);
    }//根据故障代码返回维修建议及置信值
}
